package com.fast.develop.integration.web;

import com.fast.develop.core.enums.RequestMethod;
import com.fast.develop.core.properties.ApiProperties;
import com.fast.develop.core.properties.DomainRuntimeProperties;
import com.fast.develop.core.vo.PageReq;
import com.fast.develop.core.web.AbstractMappingRegistor;
import com.fast.develop.core.web.MappingInfo;
import lombok.SneakyThrows;
import lombok.extern.slf4j.Slf4j;

import java.util.Map;

/**
 * @author 莫寅
 * @createTime 2022-06-05 23:21
 * @desc：
 */
@Slf4j
public class DomainRequestMappingHandlerMapping extends AbstractMappingRegistor {


    public static final String KEY = "/meta";

    public DomainRequestMappingHandlerMapping(ApiProperties apiProperties, MvcMappingHandlerMapping mvcMappingHandlerMapping, DomainModelResourcesRest domainModelResourcesRest) {
        super(mvcMappingHandlerMapping, apiProperties, domainModelResourcesRest);
    }

    @Override
    @SneakyThrows
    public void registerMapping() {
        // 注册资源查询
        MappingInfo mappingInfo = new MappingInfo(new String[]{getPath("{model}")}, new RequestMethod[]{RequestMethod.GET});
        registerMapping(mappingInfo, DomainModelResourcesRest.class.getDeclaredMethod("resources", String.class, PageReq.class));

        // 注册创建资源
        MappingInfo createInfo = new MappingInfo(new String[]{getPath("{model}")}, new RequestMethod[]{RequestMethod.POST});
        registerMapping(createInfo, DomainModelResourcesRest.class.getDeclaredMethod("create", String.class, Map.class));

        // 注册修改资源
        MappingInfo updateInfo = new MappingInfo(new String[]{getPath("{model}/{id}")}, new RequestMethod[]{RequestMethod.PUT});
        registerMapping(updateInfo, DomainModelResourcesRest.class.getDeclaredMethod("update", String.class, String.class, Map.class));

        // 注册删除资源
        MappingInfo deleteInfo = new MappingInfo(new String[]{getPath("{model}/{id}")}, new RequestMethod[]{RequestMethod.DELETE});
        registerMapping(deleteInfo, DomainModelResourcesRest.class.getDeclaredMethod("delete", String.class, String.class));

        // 注册获取资源
        MappingInfo getInfo = new MappingInfo(new String[]{getPath("{model}/{id}")}, new RequestMethod[]{RequestMethod.GET});
        registerMapping(getInfo, DomainModelResourcesRest.class.getDeclaredMethod("get", String.class, String.class));
    }


    @SneakyThrows
    public void unRegisterResourcesMapping(){
        // 注册资源查询
        MappingInfo mappingInfo = new MappingInfo(new String[]{getPath("{model}")}, new RequestMethod[]{RequestMethod.GET});
        unRegisterMapping(mappingInfo);

        // 注册创建资源
        MappingInfo createInfo = new MappingInfo(new String[]{getPath("{model}")}, new RequestMethod[]{RequestMethod.POST});
        unRegisterMapping(createInfo);

        // 注册修改资源
        MappingInfo updateInfo = new MappingInfo(new String[]{getPath("{model}/{id}")}, new RequestMethod[]{RequestMethod.PUT});
        unRegisterMapping(updateInfo);

        // 注册删除资源
        MappingInfo deleteInfo = new MappingInfo(new String[]{getPath("{model}/{id}")}, new RequestMethod[]{RequestMethod.DELETE});
        unRegisterMapping(deleteInfo);

        // 注册获取资源
        MappingInfo getInfo = new MappingInfo(new String[]{getPath("{model}/{id}")}, new RequestMethod[]{RequestMethod.GET});
        unRegisterMapping(getInfo);
        registerMapping(getInfo, DomainModelResourcesRest.class.getDeclaredMethod("get", String.class, String.class));
    }


    @Override
    protected String getKey() {
        return KEY;
    }


    @Override
    public String type() {
        return KEY;
    }
}
